#import "MO2XMLEntityExporter.h"
#import "MO2XMLManagedObjectsExporter.h"

@implementation MO2XMLEntityExporter

- (id)initWithEntityName:(NSString *)value {
	self = [super init];
	if(self)
	{
		[self setEntityName:value];
		[self setManagedObjectContext:nil];
	}
	return self;
}

- (void)dealloc {
	[entityName release];
	[managedObjectContext release];
	
	[super dealloc];
}

- (NSString *)entityName {
	return entityName;
}

- (void)setEntityName:(NSString *)value {
	[value retain];
	[entityName release];
	entityName = value;
}

- (NSArray *)managedObjects {
	NSManagedObjectContext *moc = [self managedObjectContext];
	NSEntityDescription *entityDescription = [self entityDescription];
	NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
	[request setEntity:entityDescription];
 
	// Set example predicate and sort orderings...
	NSPredicate *predicate = [NSPredicate predicateWithValue:YES];
	[request setPredicate:predicate];
 
	NSError *error = nil;
	NSArray *array = [moc executeFetchRequest:request error:&error];
	if (array == nil)
	{
    // Deal with error...
	}
	return array;	
}

- (NSEntityDescription *)entityDescription {
	return [NSEntityDescription entityForName:[self entityName] inManagedObjectContext:[self managedObjectContext]];
}

- (NSArray *)managedObjectsAsDictionaries {
	MO2XMLManagedObjectsExporter *exporter = [[[MO2XMLManagedObjectsExporter alloc] init] autorelease];
	[exporter setManagedObjects:[self managedObjects]];
	return [exporter managedObjectsAsDictionaries];
}


- (BOOL)writeToFile:(NSString *)aPath atomically:(BOOL)flag {
	return [[self  managedObjectsAsDictionaries] writeToFile:aPath atomically:flag];
}

@end
